﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ARS.Control.Attributes;
using ARS.Control.Common.EntityManage;
using ARS.Control.Custom.Entity.Fields;
using ARS.Control.Custom.Entity.Model;
using ARS.Control.Interface;
using ARS.PublicTool;
using ARS.PublicTool.DataHandle;

namespace ARS.Control.Custom.Entity.Widget
{
    public class EntityModifyPanel : EntityControl
    {


        public EntityModifyPanel()
        {
            base.type = EntityModel.TYPE_MODIFY;
        }


        /// <summary>
        /// 实体数据到控件
        /// </summary>
        public override  void EntityToControl()
        {

        }
        /// <summary>
        /// 控件数据到实体
        /// </summary>
        public override void ControlToEntity()
        {
//            foreach (ARS.Control.Custom.Entity.Fields.Field control in EntityTableInfo.FieldList)
//            {
//                var item = control;
//                item.Value = item.Widget.GetInputValue();
//            }
        }


        protected override void OnPreRender(EventArgs e)
        {
            HtmlLink link1 = new HtmlLink();
            link1.Attributes["type"] = "text/css";
            link1.Attributes["rel"] = "stylesheet";
            link1.Attributes["href"] = Page.ClientScript.GetWebResourceUrl(typeof(EntityModifyPanel),
                                                ResourcePath.EasyValidator.css);
            this.Page.Header.Controls.Add(link1);

            this.Page.ClientScript.RegisterClientScriptResource(typeof(EntityModifyPanel), ResourcePath.jquery);
            this.Page.ClientScript.RegisterClientScriptResource(typeof(EntityModifyPanel), ResourcePath.EasyValidator.pack);
            this.Page.ClientScript.RegisterClientScriptResource(typeof(EntityModifyPanel), ResourcePath.EasyValidator.bgiframe);


            
            base.OnPreRender(e);
        }



        public event AddEntityToDbDelegate RenderEvent;

        /// <summary>
        /// 一行多少列
        /// </summary>
        public int ColumnNumber = 1;

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {

            this.Page.ClientScript.RegisterClientScriptResource(typeof(EntityModifyPanel), ResourcePath.TreeListView.js);

            if (this.Visible)
            {
                var list = EntityTableInfo.GetFields(base.IsAdd);
                int index = 0;
                foreach (Fields.Field field in list)
                {
                    if (RenderEvent != null)
                    {
                        RenderEvent(field);
                    }
                    if (ColumnNumber == 1)
                    {

                        this.Controls.Add(field.Widget);
                    }
                    else
                    {
                        if (index%ColumnNumber == 0)
                        {
                            Literal literal_start = new Literal();
                            literal_start.Text = "<tr>";
                            this.Controls.Add(literal_start);
                            this.Controls.Add(field.Widget);
                        }
                        else
                        {
                            this.Controls.Add(field.Widget);
                            if (index%ColumnNumber == ColumnNumber - 1 || index == list.Count - 1)
                            {
                                Literal literal_end = new Literal();
                                literal_end.Text = "</tr>";
                                this.Controls.Add(literal_end);
                            }
                        }
                        index++;
                    }

                }
            }
            base.RenderContents(writer);
        }
    
    }
}
